package com.example.demo.service.impl;


import com.example.demo.entity.Act_template;
import com.example.demo.entity.Activity;
import com.example.demo.entity.Joiner;
import com.example.demo.entity.Schedule;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.*;
import com.example.demo.service.ActivityService;
import com.example.demo.utils.ThreadLocalUtil;
import com.example.demo.vo.Activityvo;
import com.example.demo.vo.ScheduleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class ActivityServicelmpl implements ActivityService {
    @Autowired
    Act_templateRepository act_templateRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    ActivityRepository activityRepository;

    @Autowired

    ScheduleRepository scheduleRepository;
    @Autowired
    private JoinerRepository joinerRepository;

    /*
    查询所有活动模板
    */
    @Override
    public PlannerResult<List<Act_template>> allact_template() {
        List <Act_template> act_templateList = act_templateRepository.findAll();
        return PlannerResult.success(act_templateList);
    }
/*
这是新建活动方法
 */
    @Override
    public PlannerResult<Integer> create(Activityvo activityvo) {
        Map<String,Object> map= ThreadLocalUtil.get();
        Integer userid=(Integer) map.get("id");

        //建立新活动对象
        Activity activity=new Activity();
        activity.setCreator_id(userid);
        activity.setName(activityvo.getName());
        activity.setInfo(activityvo.getInfo());
        activity.setStart_time(activityvo.getStart_time());
        activity.setEnd_time(activityvo.getEnd_time());
        activity.setLocation(activityvo.getLocation());
        activity.setState("报名中");
        activity.setJoiner_number(1);
        activity.setMax_num(100);

        List<String> pictures= new ArrayList<>();
        pictures.add("https://tse1-mm.cn.bing.net/th/id/OIP-C.RI0CJU0wh4CNvWfKv00oegHaHk?w=147&h=180&c=7&r=0&o=5&dpr=1.1&pid=1.7");
        pictures.add("https://ts2.cn.mm.bing.net/th?id=OIP-C.VzhOTC3SVqdVV48AhF5grwHaFS&w=295&h=211&c=8&rs=1&qlt=90&o=6&dpr=1.1&pid=3.1&rm=2");
        pictures.add("https://tse4-mm.cn.bing.net/th/id/OIP-C.9L2IMyKH9OiYyeTXbAYy_AHaHH?w=194&h=187&c=7&r=0&o=5&dpr=1.1&pid=1.7");

        // 随机选择一张图片
        Random random = new Random();
        int randomIndex = random.nextInt(pictures.size());
        String randomPicture = pictures.get(randomIndex);

        activity.setPicture(randomPicture);
        // 保存到数据库
        activityRepository.save(activity);

        // 保存创建者到参与者表
        Joiner joiner=new Joiner();
        joiner.setActivity_id(activity.getId());
        joiner.setUser_id(activity.getCreator_id());
        joiner.setIdentity("嘉宾");
        joiner.setQuanxian("创建者");
        joinerRepository.save(joiner);

        Integer id=activityRepository.findByName(activityvo.getName()).getId();
        if(id==null)
            return PlannerResult.error("失败");
        return PlannerResult.success(id);
    }
/*
这是日程创建方法，会检测日程冲突
 */
    @Override
    public PlannerResult<String> schCreate(ScheduleVo scheduleVo) {
        //建立新日程对象
        Schedule schedule=new Schedule();
        schedule.setActivity_id(scheduleVo.getActivity_id());
        schedule.setName(scheduleVo.getName());
        schedule.setContext(scheduleVo.getContext());
        schedule.setStart_time(scheduleVo.getStart_time());
        schedule.setEnd_time(scheduleVo.getEnd_time());
        //判断日程冲突
        List<Schedule> list = scheduleRepository.findAllByActivity_id(scheduleVo.getActivity_id());
        //新增时间段
        LocalDateTime beginDate2 = scheduleVo.getStart_time();
        LocalDateTime endDate2 =scheduleVo.getEnd_time();
        for (Schedule element:list
             ) {
         //已有时间段
        LocalDateTime beginDate = element.getStart_time();
        LocalDateTime endDate = element.getEnd_time();

            if(!(beginDate.compareTo(endDate2) >= 0 || endDate.compareTo(beginDate2) <= 0))
            {
                return PlannerResult.error("日程冲突，请重新建立日程");
            }
        }

        //保存日程到数据库
        Schedule s=scheduleRepository.save(schedule);
        if(s.getId()!=null)
        return PlannerResult.success("新建日程成功");
        else
            return PlannerResult.error("新建日程失败");
    }


    //改变活动状态
    @Override
    public PlannerResult<String> setState(Integer actid,String nextstate) {

        Activity activity= activityRepository.findById(actid).get();
        activity.setState(nextstate);
        if(activity.getState().equals(nextstate))
        return PlannerResult.success(nextstate);
        else
            return PlannerResult.error("修改失败");
    }

    @Override
    public PlannerResult<String> update(Activityvo activityvo) {
        Activity activity=activityRepository.findById((activityvo.getId())).get();
        if(activity.getId() == null)
            return PlannerResult.error("失败");

        activity.setName(activityvo.getName());
        activity.setInfo(activityvo.getInfo());
        activity.setStart_time(activityvo.getStart_time());
        activity.setEnd_time(activityvo.getEnd_time());
        activity.setLocation(activityvo.getLocation());
        activity.setState(activityvo.getState());
        activity.setMax_num(activityvo.getMax_num());

        //保存到数据库
        activity = activityRepository.save(activity);
        if(activity.getId()==null)
            return PlannerResult.error("更新失败");
        return PlannerResult.success("更新成功");
    }

    @Override
    public PlannerResult<List<Activity>> findByKey(String keystring) {

        List<Activity> result = activityRepository.getByNameLike(keystring);
        if(result!=null)
        return PlannerResult.success(result);
        else
            return PlannerResult.error("没有对应活动");
    }
}
